package com.itzhoun.utils;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author 周楠
 * @Date 2022/07/11/下午 10:22
 */
@Slf4j
public class FileUtils {

    /**
     *
     * @description: 将输入流输出到页面
     * @author: Jeff
     * @date: 2019年12月7日
     * @param resp
     * @param inputStream
     */
    public static void writeFile(HttpServletResponse resp, InputStream inputStream) {
        OutputStream out = null;
        try {
            out = resp.getOutputStream();
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = inputStream.read(b)) != -1) {
                out.write(b, 0, len);
            }
            out.flush();
        } catch (IOException e) {
            log.error("输出流转换异常,异常信息：{}",e.getMessage());
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                log.error("输出流关闭异常，异常信息:{}",e.getMessage());
            }
        }
    }

}
